
package com.nn.smart.bus.controller;

import com.nn.smart.bus.commons.tools.annotation.LogOperation;
import com.nn.smart.bus.commons.tools.utils.Result;
import com.nn.smart.bus.commons.tools.validator.AssertUtils;
import com.nn.smart.bus.commons.tools.validator.ValidatorUtils;
import com.nn.smart.bus.commons.tools.validator.group.AddGroup;
import com.nn.smart.bus.commons.tools.validator.group.DefaultGroup;
import com.nn.smart.bus.commons.tools.validator.group.UpdateGroup;
import com.nn.smart.bus.dto.epdc.form.UserSysDeptInfoFormDTO;
import com.nn.smart.bus.dto.epdc.result.UserSysDeptInfoResultDTO;
import com.nn.smart.bus.service.SysDeptService;
import com.nn.smart.bus.dto.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 部门管理
 *
 * @author nanning
 */
@RestController
@RequestMapping("dept")
@Api(tags = "部门管理")
public class SysDeptController {
    @Autowired
    private SysDeptService sysDeptService;

    @GetMapping("list")
    @ApiOperation("列表")
    public Result<List<SysDeptDTO>> list() {
        List<SysDeptDTO> list = sysDeptService.list(new HashMap<>(1));

        return new Result<List<SysDeptDTO>>().ok(list);
    }

    @GetMapping("sublist/{pid}")
    @ApiOperation("子列表")
    public Result<List<SysSimpleDeptDTO>> sublist(@PathVariable("pid") String pid) {
        List<SysSimpleDeptDTO> list = sysDeptService.subList(pid);
        return new Result().ok(list);
    }

    /***
     *  通过编码得到党委编码
     * @param partyCode
     * @return com.nn.smart.bus.commons.tools.utils.Result<java.util.Map < java.lang.String, java.lang.String>>
     * @author nanning
     */
    @GetMapping("getIdByCode/{partyCode}")
    public Result<Map<String, String>> getIdByCode(@PathVariable("partyCode") String partyCode) {
        return new Result<Map<String, String>>().ok(sysDeptService.getIdByCode(partyCode));
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    public Result<SysDeptDTO> get(@PathVariable("id") Long id) {
        SysDeptDTO data = sysDeptService.get(id);

        return new Result<SysDeptDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("Save Dept")
    public Result save(@RequestBody SysDeptDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        sysDeptService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("Update Dept")
    public Result update(@RequestBody SysDeptDTO dto) {
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        sysDeptService.update(dto);
        return new Result();
    }

    @DeleteMapping("{id}")
    @ApiOperation("删除")
    @LogOperation("Delete Dept")
    public Result delete(@PathVariable("id") Long id) {
        AssertUtils.isNull(id, "id");
        sysDeptService.deleteBatchIds(Arrays.asList(new Long[]{id}));
        return new Result();
    }

    @GetMapping("listGridId/{pid}")
    public Result<List<Long>> listGridIdByPid(@PathVariable("pid") Long pid) {
        return sysDeptService.listGridIdByPid(pid);
    }

    /**
     * 通过网格ID获取该网格所有上级机构
     *
     * @param gridId
     * @return com.nn.smart.bus.commons.tools.utils.Result<com.nn.smart.bus.dto.CompleteDeptDTO>
     * @author nanning
     */
    @GetMapping("getCompleteDept/{gridId}")
    public Result<CompleteDeptDTO> getCompleteDept(@PathVariable("gridId") String gridId) {
        return sysDeptService.getCompleteDept(gridId);
    }
    /**
     * 通过网格ID获取该网格所有上级机构
     *
     * @param deptId
     * @return com.nn.smart.bus.commons.tools.utils.Result<com.nn.smart.bus.dto.CompleteDeptDTO>
     * @author nanning
     */
    @GetMapping("getParentAndAllDept/{deptId}")
    public Result<ParentAndAllDeptDTO> getParentAndAllDept(@PathVariable("deptId") String deptId) {
        return sysDeptService.getParentAndAllDept(deptId);
    }

    /**
     * 根据部门id，获取所有上级部门id，英文逗号隔开拼接字符串
     *
     * @param deptId
     * @return com.nn.smart.bus.commons.tools.utils.Result<java.lang.String>
     * @author nanning
     */
    @GetMapping("getParentDeptIds/{deptId}")
    public Result<String> getParentDeptIds(@PathVariable("deptId") Long deptId) {
        return sysDeptService.getParentDeptIds(deptId);
    }

    /**
     * 根据部门id，获取部门所在四级机构的所有机构名称
     *
     * @param deptId
     * @return com.nn.smart.bus.commons.tools.utils.Result<java.util.List < java.lang.String>>
     * @author nanning
     * @date 2019/11/27 15:08
     */
    @GetMapping("getCompleteDeptList/{deptId}")
    public Result<List<String>> getCompleteDeptList(@PathVariable("deptId") Long deptId) {
        return sysDeptService.getCompleteDeptList(deptId);
    }

    /**
     * @param formDTO
     * @return com.nn.smart.bus.commons.tools.utils.Result<java.util.List < com.nn.smart.bus.dto.epdc.result.UserSysDeptInfoResultDTO>>
     * @author nanning
     * @Description 获取用户数据权限下各部门基本信息
     **/
    @PostMapping("queryUserSysDeptInfo")
    public Result<List<UserSysDeptInfoResultDTO>> queryUserSysDeptInfo(@RequestBody UserSysDeptInfoFormDTO formDTO) {
        ValidatorUtils.validateEntity(formDTO);
        List<UserSysDeptInfoResultDTO> list = sysDeptService.listUserSysDeptInfoResultDTO(formDTO);
        return new Result<List<UserSysDeptInfoResultDTO>>().ok(list);
    }

    /***
     * 获取所有部门树
     * @param
     * @return com.nn.smart.bus.commons.tools.utils.Result<com.nn.smart.bus.dto.DeptOption>
     * @author nanning
     * @date 2019/12/13 15:25
     */
    @GetMapping("party/getDeptTree")
    public Result<DeptOption> getDeptTree() {
        return sysDeptService.getDeptTree();
    }

    /**
     * @param
     * @return com.nn.smart.bus.commons.tools.utils.Result<com.nn.smart.bus.dto.DeptOption>
     * @author nanning
     * @Description 获取所有部门树:二级只要街道
     **/
    @GetMapping("party/getDeptTreeForEpiDemic")
    public Result<DeptOption> getDeptTreeForEpiDemic() {
        return sysDeptService.getDeptTreeForEpiDemic();
    }
}
